import Http from "../utils/Http";
import Crypt from "../utils/Crypt";

export default class UserModel extends Http {

  getAgreement(data) {
    return this.request({
      url: '/h5/mini/user/agreement',
      data
    })
  }

  // get app set
  getSet(data) {
    return this.request({
      url: '/h5/mini/user/get-set',
      data
    })
  }

  // login, get token
  login(data) {
    return this.request({
      url: '/h5/mini/user/login',
      data
    })
  }

  // save avatar, nickname
  saveUserInfo(data) {
    return this.request({
      url: '/h5/mini/user/save-info',
      data
    })
  }

  // save mobile
  saveMobile(data) {
    return this.request({
      url: '/h5/mini/user/save-mobile',
      data
    })
  }

  // check login
  checkLogin(isRedirect = true) {
    return new Promise(resolve => {
      this.request({
        url: '/h5/execute/user/is-login',
        data: {
          token: this.getToken()
        }
      }).then(response => {
        let { data } = response
        data = Crypt.parse(data)
        if (data.isLogin) {
          resolve()
        } else {
          if (isRedirect) {
            wx.showModal({
              title: '需要登录才能继续,是否立即登录?',
              success: res => {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/sub_main/pages/user/login/index'
                  })
                }
              }
            })
          }
        }
      })
    })
  }

  // get info
  getInfo() {
    return this.request({
      url: '/h5/user/get-info',
    })
  }

  // pay password
  checkPayPassword(data) {
    return this.request({
      url: '/h5/execute/user/check-pay-password',
      data
    })
  }

  editPayPassword(data) {
    return this.request({
      url: '/h5/execute/user/edit-pay-password',
      data
    })
  }

  verifyPayPassword(data) {
    return this.request({
      url: '/h5/execute/user/verify-pay-password',
      data
    })
  }

  // bank
  bankFetch(data) {
    return this.request({
      url: '/h5/execute/user/fetch-bank',
      data
    })
  }

  bankEdit(data) {
    return this.request({
      url: '/h5/execute/user/edit-bank',
      data
    })
  }

  // qrcode
  qrcodeList(data) {
    return this.request({
      url: '/h5/mini/mini-qrcode/get-qrcode',
      data
    })
  }

  getCert(data) {
    return this.request({
      url: '/h5/mini/mini-qrcode/get-cert',
      data
    })
  }

  // user buy
  addCart(data) {
    return this.request({
      url: '/h5/cart/add-cart',
      data
    })
  }

  // team
  teamList(data) {
    return this.request({
      url: '/h5/my/team/list',
      data
    })
  }

  teamTotal(data) {
    return this.request({
      url: '/h5/my/team/total',
      data
    })
  }

  // commission
  commissionList(data) {
    return this.request({
      url: '/h5/my/commission/list',
      data
    })
  }

  // account
  myAccount(data) {
    return this.request({
      url: '/h5/my/account/account',
      data
    })
  }

  myCommission(data) {
    return this.request({
      url: '/h5/my/account/commission',
      data
    })
  }

  myConsume(data) {
    return this.request({
      url: '/h5/my/account/consume',
      data
    })
  }

  // 后台充值
  myRefill(data) {
    return this.request({
      url: '/h5/my/account/refill',
      data
    })
  }

  // 明细
  myBalanceLog(data) {
    return this.request({
      url: '/h5/my/account/balance-log',
      data
    })
  }

  // cash
  cashList(data) {
    return this.request({
      url: '/h5/my/cash/list',
      data
    })
  }

  cashFetch(data) {
    return this.request({
      url: '/h5/my/cash/fetch',
      data
    })
  }

  cashSave(data) {
    return this.request({
      url: '/h5/my/cash/save',
      data
    })
  }

  // 申请区代
  regArea(data) {
    return this.request({
      url: '/h5/my/account/reg-area',
      data
    })
  }

  // product qrcode
  qrcodeShare(data) {
    return this.request({
      url: '/h5/product/qrcode-share-mini',
      data
    })
  }

  // update avatar
  changeAvatar(data) {
    return this.request({
      url: '/h5/mini/user/change-avatar',
      data
    })
  }

  // --------------------------------

  // 提现时检查支付密码
  checkSetPassword(data) {
    return this.request({
      url: '/h5/mini/user/check-set-password',
      data
    })
  }

  // 复购时, 检查是否报单
  checkRepeatOk(data) {
    return this.request({
      url: '/h5/cart/check-repeat',
      data
    })
  }

  // 互转
  onTransfer(data) {
    return this.request({
      url: '/h5/transfer/execute',
      data
    })
  }
}
